// 委案商数据管理工具
import institutionData from './institution.json';

// 委案商数据类型定义
export interface Institution {
  customerId: string;
  customerName: string;
}

export class InstitutionManager {
  private static instance: InstitutionManager;
  private institutions: Institution[] = [...institutionData];

  private constructor() {}

  public static getInstance(): InstitutionManager {
    if (!InstitutionManager.instance) {
      InstitutionManager.instance = new InstitutionManager();
    }
    return InstitutionManager.instance;
  }

  // 获取所有委案商
  public getAllInstitutions(): Institution[] {
    return [...this.institutions];
  }

  // 根据机构名称获取customerId
  public getCustomerIdByName(customerName: string): string | null {
    const institution = this.institutions.find(inst => inst.customerName === customerName);
    return institution ? institution.customerId : null;
  }

  // 根据customerId获取机构名称
  public getCustomerNameById(customerId: string): string | null {
    const institution = this.institutions.find(inst => inst.customerId === customerId);
    return institution ? institution.customerName : null;
  }

  // 搜索委案商
  public searchInstitutions(keyword: string): Institution[] {
    if (!keyword.trim()) {
      return this.getAllInstitutions();
    }
    
    const lowerKeyword = keyword.toLowerCase();
    return this.institutions.filter(inst => 
      inst.customerName.toLowerCase().includes(lowerKeyword) ||
      inst.customerId.includes(keyword)
    );
  }

  // 添加委案商
  public addInstitution(institution: Institution): boolean {
    const existing = this.institutions.find(inst => inst.customerId === institution.customerId);
    if (existing) {
      console.warn(`委案商ID ${institution.customerId} 已存在`);
      return false;
    }
    
    this.institutions.push(institution);
    console.log(`成功添加委案商: ${institution.customerName} (${institution.customerId})`);
    return true;
  }

  // 更新委案商
  public updateInstitution(customerId: string, updates: Partial<Institution>): boolean {
    const institution = this.institutions.find(inst => inst.customerId === customerId);
    if (!institution) {
      console.warn(`委案商ID ${customerId} 不存在`);
      return false;
    }
    
    Object.assign(institution, updates);
    console.log(`成功更新委案商: ${institution.customerName} (${institution.customerId})`);
    return true;
  }

  // 删除委案商
  public removeInstitution(customerId: string): boolean {
    const index = this.institutions.findIndex(inst => inst.customerId === customerId);
    if (index === -1) {
      console.warn(`委案商ID ${customerId} 不存在`);
      return false;
    }
    
    const removed = this.institutions.splice(index, 1)[0];
    console.log(`成功删除委案商: ${removed.customerName} (${removed.customerId})`);
    return true;
  }

  // 获取委案商统计信息
  public getStatistics() {
    return {
      total: this.institutions.length,
      customerIds: this.institutions.map(inst => inst.customerId),
      customerNames: this.institutions.map(inst => inst.customerName)
    };
  }

  // 导出委案商数据
  public exportToJSON(): string {
    return JSON.stringify(this.institutions, null, 2);
  }

  // 从JSON导入委案商数据
  public importFromJSON(jsonData: string): boolean {
    try {
      const data = JSON.parse(jsonData);
      if (Array.isArray(data)) {
        this.institutions = data;
        console.log(`成功导入 ${data.length} 个委案商`);
        return true;
      }
      throw new Error('无效的JSON格式');
    } catch (error) {
      console.error('导入委案商数据失败:', error);
      return false;
    }
  }

  // 重置为默认配置
  public resetToDefault(): void {
    this.institutions = [...institutionData];
    console.log('已重置为默认委案商配置');
  }
}

// 导出单例实例
export const institutionManager = InstitutionManager.getInstance();
